/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pwned;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import pwned.memory.Scanner;
import pwned.objects.Target;

/**
 *
 * @author Zlobniy
 */
public class Pwned {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        MyAddresses addresses = new MyAddresses();
        
        final Scanner scan = new Scanner( addresses.getOffsets() );
        scan.setTimeBetweenScan(5000l);
        Thread scannerthread = new Thread(scan);
        scannerthread.start();
        
        Pwned pwned = new Pwned();
        pwned.makeSimpleWindow(scan);
        
        while(true){
            Thread.currentThread().setName("Main Thread");
            System.out.println("thread "+Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
                
                System.out.println("scan.getCount: "+scan.getCount());
                
                int i = 1;
                for( Target target : scan.getTargets() ){
                    
                    System.out.println("Target: "+target.toString()+" isNew: "+ target.isNewTarget() +" size "+ scan.getTargets().size());
                    i++;
                }
                
                Thread.sleep(1000);
                
            } catch (InterruptedException ex) {
                Logger.getLogger(Pwned.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private void makeSimpleWindow( final Scanner scan ){
        
        JFrame frame = new JFrame();
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLayout( null );
        
        addButton( frame, scan );
        
    }
    
    private void addButton( JFrame frame, final Scanner scan ){
        
        final JButton button = new JButton("Start");
        button.setBounds(20, 20, 100, 20);
        button.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("start/stop");
                if( button.getText().equalsIgnoreCase("Start") ){
                    button.setText("Stop");
                    System.out.println("Stop");
                    if( !scan.isRunning() ){
                        scan.setRunning(true);
                        System.out.println("isRunning "+scan.isRunning());
                    }
                }else{
                    button.setText("Start");
                    System.out.println("Start");
                    if( scan.isRunning() ){
                        scan.setRunning(false);
                        System.out.println("isRunning "+scan.isRunning());
                    }
                }
                
            }
        });
        frame.add(button);
        
    }
    
}
